package com.xian.im.modules.command.group.handler;

import com.xian.im.common.constant.CommandCodeConstant;
import com.xian.im.common.model.dto.GroupDto;
import com.xian.im.common.model.session.SessionUser;
import com.xian.im.common.proto.MsgRequest;
import com.xian.im.common.proto.MsgResponse;
import com.xian.im.common.utils.bussiness.ChannelClientContextUtil;
import com.xian.im.common.utils.bussiness.GroupContextUtil;
import com.xian.im.common.utils.bussiness.XimResponseUtil;
import com.xian.im.common.utils.sys.SpringContextHolder;
import com.xian.im.modules.command.group.AbsGroupCommand;
import com.xian.im.modules.im.core.IGroupUserCoreService;
import io.netty.channel.ChannelHandlerContext;

/**
 * @program: xian-im-server
 * @description: 解除管理员身份
 * @author: liru.xian
 * @create: 2021-01-28 22:54
 **/
public class GroupRevokeManager extends AbsGroupCommand {

    @Override
    public void handler(MsgRequest request, String ctx, GroupDto groupEntity) {
        ChannelHandlerContext ctxTo = ChannelClientContextUtil.getCtxById(request.getToAccount());
        SessionUser sessionUser  = userChannelClient().sessionUser(request.getToAccount());
        String account = sessionUser.getAccount();
        SpringContextHolder.getBean(IGroupUserCoreService.class).removeManager(account, groupEntity.getGroupUniqueId());
        GroupContextUtil.getSessionGroup(groupEntity.getGroupUniqueId()).getManagers().remove(account);
        SessionUser managerSession = userChannelClient().session(ctx);
        MsgResponse response = XimResponseUtil.buildSuccess(managerSession);
        response.setGroupUniqueId(groupEntity.getGroupUniqueId());
        response.setBody(sessionUser.getNickname() + " 被" + managerSession.getNickname() + "解除管理员");

        //发送消息
        groupChannelClient().pushGroupSystemMessage( groupEntity.getGroupUniqueId(),response);
    }

    /**
     * 群组code
     *
     * @return
     */
    @Override
    public String cmdCode() {
        return CommandCodeConstant.GROUP_REVOKE_MANAGER;
    }
}
